home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / ui / listbox.c < prev    next >
C/C++ Source or Header  |  1998-08-08  |  10KB  |  397 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13.  
  14. #include <stdlib.h>
  15.  
  16. #include "fix.h"
  17. #include "types.h"
  18. #include "gr.h"
  19. #include "ui.h"
  20. #include "key.h"
  21. #include "timer.h"
  22.  
  23. void ui_draw_listbox( UI_GADGET_LISTBOX * listbox )
  24. {
  25.     int i, x, y, stop;
  26.     int w, h,  aw;
  27.  
  28.     //if (listbox->current_item<0)
  29.     //    listbox->current_item=0;
  30.     //if (listbox->current_item>=listbox->num_items)
  31.     //    listbox->current_item = listbox->num_items-1;
  32.     //if (listbox->first_item<0)
  33.     //   listbox->first_item=0;
  34.     //if (listbox->first_item>(listbox->num_items-listbox->num_items_displayed))
  35.     //    listbox->first_item=(listbox->num_items-listbox->num_items_displayed);
  36.  
  37.     if ((listbox->status!=1) && !listbox->moved )
  38.         return;
  39.  
  40.     stop = listbox->first_item+listbox->num_items_displayed;
  41.     if (stop>listbox->num_items) stop = listbox->num_items;
  42.  
  43.     listbox->status = 0;
  44.  
  45.     x = y = 0;
  46.     ui_mouse_hide();
  47.     gr_set_current_canvas( listbox->canvas );
  48.  
  49.     for (i= listbox->first_item; i< stop; i++ )
  50.     {
  51.         if (i !=listbox->current_item)
  52.         {
  53.             if ((listbox->current_item == -1) && (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox) && (i == listbox->first_item)  )
  54.                 gr_set_fontcolor( CRED, CBLACK );
  55.             else
  56.                 gr_set_fontcolor( CWHITE, CBLACK );
  57.         }
  58.         else
  59.         {
  60.             if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
  61.                 gr_set_fontcolor( CRED, CGREY );
  62.             else
  63.                 gr_set_fontcolor( CBLACK, CGREY );
  64.         }
  65.         gr_string( x+2, y, listbox->list+i*listbox->text_width );
  66.         gr_get_string_size(listbox->list+i*listbox->text_width, &w, &h,&aw );
  67.  
  68.         if (i==listbox->current_item)
  69.             gr_setcolor( CGREY );
  70.         else
  71.             gr_setcolor( CBLACK );
  72.  
  73.         gr_rect( x+w+2, y, listbox->width-1, y+h-1 );
  74.         gr_rect( x, y, x+1, y+h-1 );
  75.  
  76.         y += h;
  77.     }
  78.  
  79.     if (stop < listbox->num_items_displayed-1 )
  80.     {
  81.         gr_setcolor(CBLACK);
  82.         gr_rect( x, y, listbox->width-1, listbox->height-1 );
  83.     }
  84.  
  85.     //gr_ubox( -1, -1, listbox->width, listbox->height);
  86.     ui_mouse_show();
  87.  
  88. }
  89.  
  90.  
  91. void gr_draw_sunken_border( short x1, short y1, short x2, short y2 )
  92. {
  93.  
  94.     gr_setcolor( CGREY );
  95.     Hline( x1-1, x2+1, y1-1);
  96.     Vline( y1-1, y2+1, x1-1);
  97.  
  98.     gr_setcolor( CBRIGHT );
  99.     Hline( x1-1, x2+1, y2+1);
  100.     Vline( y1, y2+1, x2+1);
  101.  
  102. }
  103.  
  104.  
  105. UI_GADGET_LISTBOX * ui_add_gadget_listbox( UI_WINDOW * wnd, short x, short y, short w, short h, short numitems, char * list, int text_width )
  106. {
  107.     int tw, th, taw, i;
  108.  
  109.     UI_GADGET_LISTBOX * listbox;
  110.  
  111.     gr_get_string_size("*", &tw, &th, &taw );
  112.  
  113.     i = h / th;
  114.     h = i * th;
  115.  
  116.     listbox = (UI_GADGET_LISTBOX *)ui_gadget_add( wnd, 2, x, y, x+w-1, y+h-1 );
  117.  
  118.     listbox->list = list;
  119.     listbox->text_width = text_width;
  120.     listbox->width = w;
  121.     listbox->height = h;
  122.     listbox->num_items = numitems;
  123.     listbox->num_items_displayed = i;
  124.     listbox->first_item = 0;
  125.     listbox->current_item = -1;
  126.     listbox->last_scrolled = 0;
  127.     listbox->textheight = th;
  128.     listbox->dragging = 0;
  129.     listbox->selected_item = -1;
  130.     listbox->moved = 1;
  131.  
  132.     listbox->scrollbar = ui_add_gadget_scrollbar( wnd, x+w+3, y, 0, h, 0, numitems-i, 0, i );
  133.     listbox->scrollbar->parent = (UI_GADGET *)listbox;
  134.  
  135.     gr_set_current_canvas( listbox->canvas );
  136.  
  137.     gr_setcolor(CBLACK);
  138.     gr_rect( 0, 0, w-1, h-1);
  139.  
  140.     gr_draw_sunken_border( -2, -2, w+listbox->scrollbar->width+4, h+1);
  141.  
  142.     return listbox;
  143.  
  144. }
  145.  
  146. void ui_listbox_do( UI_GADGET_LISTBOX * listbox, int keypress )
  147. {
  148.     int OnMe, mitem, oldfakepos, kf;
  149.  
  150.     listbox->selected_item = -1;
  151.  
  152.     listbox->moved = 0;
  153.  
  154.     if (listbox->num_items < 1 ) {
  155.         listbox->current_item = -1;
  156.         listbox->first_item = 0;
  157.         listbox->old_current_item = listbox->current_item;
  158.         listbox->old_first_item = listbox->first_item;
  159.         ui_draw_listbox( listbox );
  160.  
  161.         if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)listbox)
  162.         {
  163.             CurWindow->keyboard_focus_gadget = ui_gadget_get_next((UI_GADGET *)listbox);
  164.         }
  165.  
  166.         return;
  167.     }
  168.  
  169.     listbox->old_current_item = listbox->current_item;
  170.     listbox->old_first_item = listbox->first_item;
  171.  
  172.     OnMe = ui_mouse_on_gadget( (UI_GADGET *)listbox );
  173.  
  174.  
  175.     if (listbox->scrollbar->moved )
  176.     {
  177.         listbox->moved = 1;
  178.  
  179.         listbox->first_item = listbox->scrollbar->position;
  180.  
  181.         if (listbox->current_item<listbox->first_item)
  182.             listbox->current_item = listbox->first_item;
  183.  
  184.         if (listbox->current_item>(listbox->first_item+listbox->num_items_displayed-1))
  185.             listbox->current_item = listbox->first_item + listbox->num_items_displayed-1;
  186.  
  187.     }
  188.  
  189.     if (!B1_PRESSED )
  190.         listbox->dragging = 0;
  191.  
  192.     if (B1_PRESSED && OnMe )
  193.         listbox->dragging = 1;
  194.  
  195.     if ( CurWindow->keyboard_focus_gadget==(UI_GADGET *)listbox )
  196.     {
  197.         if (keypress==KEY_ENTER)   {
  198.             listbox->selected_item = listbox->current_item;
  199.         }
  200.  
  201.         kf = 0;
  202.  
  203.         switch(keypress)
  204.         {
  205.             case (KEY_UP):
  206.                 listbox->current_item--;
  207.                 kf = 1;
  208.                 break;
  209.             case (KEY_DOWN):
  210.                 listbox->current_item++;
  211.                 kf = 1;
  212.                 break;
  213.             case (KEY_HOME):
  214.                 listbox->current_item=0;
  215.                 kf = 1;
  216.                 break;
  217.             case (KEY_END):
  218.                 listbox->current_item=listbox->num_items-1;
  219.                 kf = 1;
  220.                 break;
  221.             case (KEY_PAGEUP):
  222.                 listbox->current_item -= listbox->num_items_displayed;
  223.                 kf = 1;
  224.                 break;
  225.             case (KEY_PAGEDOWN):
  226.                 listbox->current_item += listbox->num_items_displayed;
  227.                 kf = 1;
  228.                 break;
  229.         }
  230.  
  231.         if (kf==1)
  232.         {
  233.             listbox->moved = 1;
  234.  
  235.             if (listbox->current_item<0)
  236.                 listbox->current_item=0;
  237.  
  238.             if (listbox->current_item>=listbox->num_items)
  239.                 listbox->current_item = listbox->num_items-1;
  240.  
  241.             if (listbox->current_item<listbox->first_item)
  242.                 listbox->first_item = listbox->current_item;
  243.  
  244.             if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
  245.                 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
  246.  
  247.             if (listbox->num_items <= listbox->num_items_displayed )
  248.                 listbox->first_item = 0;
  249.             else
  250.             {
  251.                 oldfakepos = listbox->scrollbar->position;
  252.                 listbox->scrollbar->position = listbox->first_item;
  253.  
  254.                 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
  255.                 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
  256.                 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
  257.  
  258.                 if (listbox->scrollbar->fake_position<0)
  259.                 {
  260.                     listbox->scrollbar->fake_position = 0;
  261.                 }
  262.                 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
  263.                 {
  264.                     listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
  265.                 }
  266.  
  267.                 if (oldfakepos != listbox->scrollbar->position )
  268.                     listbox->scrollbar->status = 1;
  269.             }
  270.         }
  271.     }
  272.  
  273.  
  274.     if (selected_gadget==(UI_GADGET *)listbox)
  275.     {
  276.         if (B1_PRESSED && listbox->dragging)
  277.         {
  278.             if (Mouse.y < listbox->y1)
  279.                 mitem = -1;
  280.             else
  281.                 mitem = (Mouse.y - listbox->y1)/listbox->textheight;
  282.  
  283.             if  ( (mitem < 0 ) && ( TICKER > listbox->last_scrolled+1) )
  284.             {
  285.                 listbox->current_item--;
  286.                 listbox->last_scrolled = TICKER;
  287.                 listbox->moved = 1;
  288.             }
  289.  
  290.             if ( ( mitem >= listbox->num_items_displayed ) &&
  291.                  ( TICKER > listbox->last_scrolled+1)         )
  292.             {
  293.                 listbox->current_item++;
  294.                 listbox->last_scrolled = TICKER;
  295.                 listbox->moved = 1;
  296.             }
  297.  
  298.             if ((mitem>=0) && (mitem<listbox->num_items_displayed))
  299.             {
  300.                 listbox->current_item = mitem+listbox->first_item;
  301.                 listbox->moved=1;
  302.             }
  303.  
  304.             if (listbox->current_item <0 )
  305.                 listbox->current_item = 0;
  306.  
  307.             if (listbox->current_item >= listbox->num_items )
  308.                 listbox->current_item = listbox->num_items-1;
  309.  
  310.             if (listbox->current_item<listbox->first_item)
  311.                 listbox->first_item = listbox->current_item;
  312.  
  313.             if (listbox->current_item>=(listbox->first_item+listbox->num_items_displayed))
  314.                 listbox->first_item = listbox->current_item-listbox->num_items_displayed+1;
  315.  
  316.             if (listbox->num_items <= listbox->num_items_displayed )
  317.                 listbox->first_item = 0;
  318.             else
  319.             {
  320.                 oldfakepos = listbox->scrollbar->position;
  321.                 listbox->scrollbar->position = listbox->first_item;
  322.  
  323.                 listbox->scrollbar->fake_position = listbox->scrollbar->position-listbox->scrollbar->start;
  324.                 listbox->scrollbar->fake_position *= listbox->scrollbar->height-listbox->scrollbar->fake_size;
  325.                 listbox->scrollbar->fake_position /= (listbox->scrollbar->stop-listbox->scrollbar->start);
  326.  
  327.                 if (listbox->scrollbar->fake_position<0)
  328.                 {
  329.                     listbox->scrollbar->fake_position = 0;
  330.                 }
  331.                 if (listbox->scrollbar->fake_position > (listbox->scrollbar->height-listbox->scrollbar->fake_size))
  332.                 {
  333.                     listbox->scrollbar->fake_position = (listbox->scrollbar->height-listbox->scrollbar->fake_size);
  334.                 }
  335.  
  336.                 if (oldfakepos != listbox->scrollbar->position )
  337.                     listbox->scrollbar->status = 1;
  338.             }
  339.  
  340.         }
  341.  
  342.         if (B1_DOUBLE_CLICKED )
  343.         {
  344.             listbox->selected_item = listbox->current_item;
  345.         }
  346.  
  347.     }
  348.  
  349.     ui_draw_listbox( listbox );
  350.  
  351. }
  352.  
  353. void ui_listbox_change( UI_WINDOW * wnd, UI_GADGET_LISTBOX * listbox, short numitems, char * list, int text_width )
  354. {
  355.     int stop, start;
  356.     UI_GADGET_SCROLLBAR * scrollbar;
  357.  
  358.     wnd = wnd;
  359.  
  360.     listbox->list = list;
  361.     listbox->text_width = text_width;
  362.     listbox->num_items = numitems;
  363.     listbox->first_item = 0;
  364.     listbox->current_item = -1;
  365.     listbox->last_scrolled = TICKER;
  366.     listbox->dragging = 0;
  367.     listbox->selected_item = -1;
  368.     listbox->status = 1;
  369.     listbox->first_item = 0;
  370.     listbox->current_item = listbox->old_current_item = 0;
  371.     listbox->moved = 0;
  372.  
  373.     scrollbar = listbox->scrollbar;
  374.  
  375.     start=0;
  376.     stop= numitems - listbox->num_items_displayed;
  377.  
  378.     if (stop < start) stop = start;
  379.  
  380.     scrollbar->horz = 0;
  381.     scrollbar->start = start;
  382.     scrollbar->stop = stop;
  383.     scrollbar->fake_length = scrollbar->height;
  384.     scrollbar->fake_position =  0;
  385.     if (stop!=start)
  386.         scrollbar->fake_size = (listbox->num_items_displayed * scrollbar->height)/(stop-start+1+listbox->num_items_displayed);
  387.     else
  388.         scrollbar->fake_size = scrollbar->height;
  389.  
  390.     if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
  391.     scrollbar->dragging = 0;
  392.     scrollbar->moved=0;
  393.     scrollbar->status=1;
  394.  
  395.  
  396. }
  397.